# user：hp
# web自动化阶段 2022/5/25
# 时间 21:56
"""单列日志器"""
import logging
import logging.handlers

from config import PATH


class GetLog:
    __logger = None

    @classmethod
    def get_logger(cls, log_name="llz"):
        if cls.__logger is None:
            # 获取日志器
            cls.__logger = logging.getLogger()
            # 设置日志等级
            cls.__logger.setLevel(logging.INFO)
            # 存放日志路径
            log_path = PATH + "/log/" + log_name + ".log"
            # 控制台对象
            st = logging.StreamHandler()
            # 获取处理器(日志文件）
            th = logging.handlers.TimedRotatingFileHandler(filename=log_path,
                                                           when='midnight',
                                                           interval=1,
                                                           backupCount=7,
                                                           encoding="utf-8")
            # 获取格式器
            fmt = "%(asctime)s %(levelname)s [%(filename)s(%(funcName)s:%(lineno)d)] - %(message)s"
            fm = logging.Formatter(fmt=fmt)
            # 格式器添加到处理器中
            th.setFormatter(fm)
            st.setFormatter(fm)
            # 处理器添加到日志器中
            cls.__logger.addHandler(st)  # 控制台输出
            cls.__logger.addHandler(th)
        return cls.__logger


if __name__ == '__main__':
    log = GetLog.get_logger()
    log.info("调试信息")
